{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "12 - Translate text between languages",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4Pjmz-RORV8E"
      },
      "source": [
        "# Translate text between languages\n",
        "\n",
        "This notebook covers machine translation backed by Hugging Face models. The quality of machine translation via cloud services has come a very long way and produces high quality results. This notebook shows how the models from Hugging Face give developers a reasonable alternative for local machine translation."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Dk31rbYjSTYm"
      },
      "source": [
        "# Install dependencies\n",
        "\n",
        "Install `txtai` and all dependencies. Since this notebook is using optional pipelines, we need to install the pipeline extras package."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XMQuuun2R06J"
      },
      "source": [
        "%%capture\n",
        "!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PNPJ95cdTKSS"
      },
      "source": [
        "# Create a Translation instance\n",
        "\n",
        "The Translation instance is the main entrypoint for translating text between languages. The pipeline abstracts translating text into a one line call! \n",
        "\n",
        "The pipeline has logic to detect the input language, load the relevant model that handles translating from source to target language and return results. The translation pipeline also has built-in logic to handle splitting large text blocks into smaller sections the models can handle.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nTDwXOUeTH2-"
      },
      "source": [
        "%%capture\n",
        "\n",
        "from txtai.pipeline import Translation\n",
        "\n",
        "# Create translation model\n",
        "translate = Translation()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-vGR_piwZZO6"
      },
      "source": [
        "# Translate text\n",
        "\n",
        "The example below shows how to translate text from English to Spanish. This text is then translated back to English."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "-K2YJJzsVtfq",
        "outputId": "3beede7b-0996-466b-e922-fc3da23e7eda"
      },
      "source": [
        "translation = translate(\"This is a test translation into Spanish\", \"es\")\n",
        "translation"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'Esta es una traducción de prueba al español'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "K_UnAZQpetM8",
        "outputId": "ec3ec2ef-4f97-4529-aa08-863fdea07506"
      },
      "source": [
        "translate(translation, \"en\")"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'This is a test translation into Spanish'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4cSI8GdtjhEM"
      },
      "source": [
        "# Translating multiple languages in a single call\n",
        "\n",
        "The section below translates a single English sentence into 5 different languages. The results are then passed to a single translation call to translate back into English. The pipeline detects each input language and is able to load the relevant translation models."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8jLxGtwNf0Aj",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "68bd189a-36e6-4148-cc39-868a33e60c53"
      },
      "source": [
        "languages = [\"fr\", \"es\", \"de\", \"hi\", \"ja\"]\n",
        "\n",
        "translations = [translate(\"The sky is blue, the stars are far\", language) for language in languages]\n",
        "\n",
        "english = translate(translations, \"en\")\n",
        "\n",
        "for x, text in enumerate(translations):\n",
        "  print(\"Original Language: %s\" % languages[x])\n",
        "  print(\"Translation: %s\" % text)\n",
        "  print(\"Back to English: %s\" % english[x])\n",
        "  print()\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Original Language: fr\n",
            "Translation: Le ciel est bleu, les étoiles sont loin\n",
            "Back to English: The sky is blue, the stars are far away\n",
            "\n",
            "Original Language: es\n",
            "Translation: El cielo es azul, las estrellas están lejos.\n",
            "Back to English: The sky is blue, the stars are far away.\n",
            "\n",
            "Original Language: de\n",
            "Translation: Der Himmel ist blau, die Sterne sind weit\n",
            "Back to English: The sky is blue, the stars are wide\n",
            "\n",
            "Original Language: hi\n",
            "Translation: आकाश नीला है, तारे दूर हैं\n",
            "Back to English: Sky is blue, stars are away\n",
            "\n",
            "Original Language: ja\n",
            "Translation: 天は青い、星は遠い。\n",
            "Back to English: The heavens are blue and the stars are far away.\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Xn3SlVE1LYvm"
      },
      "source": [
        "The translation quality overall is very high. Machine translation has made giant leaps and strides the last couple of years. These models give developers a solid alternative to cloud translation services if translating on local servers is preferred."
      ]
    }
  ]
}